<template>
	<div class="sys-menu-container">
		<el-card shadow="hover" :body-style="{ paddingBottom: '0' }">
			<el-form :model="state.queryParams" ref="queryForm" :inline="true">
				<el-form-item :label="$t('system.menu.menuName')">
					<el-input v-model="state.queryParams.title" :placeholder="$t('system.menu.menuName')" clearable />
				</el-form-item>
				<el-form-item :label="$t('system.menu.type')">
          			<g-sys-dict v-model="state.queryParams.type" code="MenuTypeEnum" render-as="select" :placeholder="$t('system.menu.type')" clearable />
				</el-form-item>
				<el-form-item>
					<el-button-group>
						<el-button type="primary" icon="ele-Search" @click="handleQuery" v-auth="'sysMenu:list'"> {{ $t('system.btn.query') }} </el-button>
						<el-button icon="ele-Refresh" @click="resetQuery"> {{ $t('system.btn.reset') }} </el-button>
					</el-button-group>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" icon="ele-Plus" @click="openAddMenu" v-auth="'sysMenu:addMenu'"> {{ $t('system.btn.add') }} </el-button>
				</el-form-item>
			</el-form>
		</el-card>

		<el-card class="full-table" shadow="hover" style="margin-top: 5px">
			<el-table :data="state.menuData" v-loading="state.loading" row-key="id" :tree-props="{ children: 'children', hasChildren: 'hasChildren' }" border>
				<el-table-column :label="$t('system.menu.menuName')" header-align="center" show-overflow-tooltip>
					<template #default="scope">
						<SvgIcon :name="scope.row.icon" />
						<span class="ml10">{{ $t(scope.row.title) }}</span>
					</template>
				</el-table-column>
				<el-table-column :label="$t('system.menu.type')" width="110" align="center" show-overflow-tooltip>
					<template #default="scope">
            			<g-sys-dict v-model="scope.row.type" code="MenuTypeEnum" />
					</template>
				</el-table-column>
				<el-table-column prop="path" :label="$t('system.menu.path')" header-align="center" show-overflow-tooltip />
				<el-table-column prop="component" :label="$t('system.menu.component')" align="center" show-overflow-tooltip />
				<el-table-column prop="permission" :label="$t('system.menu.permission')" align="center" show-overflow-tooltip />
				<el-table-column prop="sort" :label="$t('system.menu.sort')" width="70" align="center" show-overflow-tooltip />
				<el-table-column :label="$t('system.menu.status')" width="80" align="center" show-overflow-tooltip>
					<template #default="scope">
						<g-sys-dict v-model="scope.row.isDisable" code="StatusEnum" />
					</template>
				</el-table-column>
				<el-table-column :label="$t('system.menu.modifyRecord')" width="100" align="center" show-overflow-tooltip>
					<template #default="scope">
						<ModifyRecord :data="scope.row" />
					</template>
				</el-table-column>
				<el-table-column :label="$t('system.menu.operate')" width="210" fixed="right" align="center" show-overflow-tooltip>
					<template #default="scope">
						<el-button icon="ele-Edit" text type="primary" @click="openEditMenu(scope.row)" v-auth="'sysMenu:updateMenu'"> {{ $t('system.menu.edit') }} </el-button>
						<el-button icon="ele-Delete" text type="danger" @click="delMenu(scope.row)" v-auth="'sysMenu:deleteMenu'"> {{ $t('system.menu.delete') }} </el-button>
						<el-button icon="ele-CopyDocument" text type="primary" @click="openCopyMenu(scope.row)" v-auth="'sysMenu:addMenu'"> {{ $t('system.dict.copy') }} </el-button>
					</template>
				</el-table-column>
			</el-table>
		</el-card>

		<EditMenu ref="editMenuRef" :title="state.editMenuTitle" :menuData="state.allMenuData" @handleQuery="handleQuery" />
	</div>
</template>

<script lang="ts" setup name="sysMenu">
import { onMounted, reactive, ref } from 'vue';
import { ElMessageBox, ElMessage } from 'element-plus';
import EditMenu from '/@/views/system/menu/component/editMenu.vue';
import ModifyRecord from '/@/components/table/modifyRecord.vue';

import { apiSysMenuListGet,apiSysMenuDeletePost} from '/@/api-services/api';

const editMenuRef = ref<InstanceType<typeof EditMenu>>();
const state = reactive({
	loading: false,
	menuData: [] as Array<any>,
	allMenuData: [] as Array<any>,
	queryParams: {
		tenantId: undefined,
		title: undefined,
		type: undefined,
	},
	editMenuTitle: '',
});

onMounted(async () => {
	handleQuery();
});

// 查询操作
const handleQuery = async () => {
	state.loading = true;
	const res = await apiSysMenuListGet(state.queryParams.title, state.queryParams.type, state.queryParams.tenantId);
	state.menuData = res.data.data ?? [];
	state.loading = false;
};

// 重置操作
const resetQuery = () => {
	state.queryParams.title = undefined;
	state.queryParams.type = undefined;
	handleQuery();
};

// 获取菜单树数据
const getMenuTreeData = async () => {
	const res = await apiSysMenuListGet(undefined, undefined, state.queryParams.tenantId);
	state.allMenuData = res.data.data ?? [];
	return state.menuData;
}

// 打开新增页面
const openAddMenu = () => {
	getMenuTreeData();
	const data = { type: 2, isHide: false, isKeepAlive: true, isAffix: false, isIframe: false, tenantId: undefined, status: 1, orderNo: 100 };
	state.editMenuTitle = $t('system.menu.addMenu');
	editMenuRef.value?.openDialog(data);
};

// 打开编辑页面
const openEditMenu = (row: any) => {
	getMenuTreeData();
	state.editMenuTitle = $t('system.menu.editMenu');
	editMenuRef.value?.openDialog(row);
};

// 打开复制页面
const openCopyMenu = (row: any) => {
	state.editMenuTitle = $t('system.menu.copyMenu');
	var copyRow = JSON.parse(JSON.stringify(row)) as any;
	copyRow.id = 0;
	copyRow.title = '';
	editMenuRef.value?.openDialog(copyRow);
};

// 删除当前行
const delMenu = (row: any) => {
	ElMessageBox.confirm($t('system.btn.deleteConfirm') + `：【${row.title}】?`, $t('system.btn.confirm'), {
		confirmButtonText: $t('system.btn.confirm'),
		cancelButtonText: $t('system.btn.undo'),
		type: 'warning',
	})
		.then(async () => {
			await apiSysMenuDeletePost({ id: row.id });
			handleQuery();
			ElMessage.success($t('system.menu.deleteSuccess'));
		})
		.catch(() => {});
};
</script>
